package com.dhcc.bpm.modules.sandbox.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dhcc.bpm.common.annotation.Log;
import com.dhcc.bpm.common.enums.BusinessType;
import com.dhcc.bpm.modules.sandbox.entity.ApiReturn;
import com.dhcc.bpm.modules.sandbox.service.IApiReturnService;
import com.dhcc.bpm.modules.sandbox.vo.ApiReturnCodeVO;
import com.dhcc.bpm.modules.sandbox.vo.ApiReturnVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.log.annotation.ApiLog;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.Map;

/**
 * @ClassName ApiReturnController
 * @Description 沙箱实例表 控制器
 * @Author sjm
 * @Date 2020/2/21 0021 15:16
 * @Version V1.0
 * @Copyright: 2020 东华软件 . All rights reserved.
 **/
@RestController
@AllArgsConstructor
@RequestMapping("/bpm-sandbox/apireturn")
@Api(value = "接口返回码关联表", tags = "接口返回码关联表接口")
public class ApiReturnController extends BladeController {

	private IApiReturnService apiReturnService;

	/**
	 * @Description 详情
	 * @Author sjm
	 * @Date 21:23 2020/2/21 0021
	 * @param apiReturn
	 * @return org.springblade.core.tool.api.R<com.dhcc.bpm.modules.sandbox.entity.ApiReturn>
	 **/
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入apiReturn")
	@ApiLog("接口返回码关联详情")
	@Log(title = "接口返回码关联详情", businessType = BusinessType.SELECT)
	public R<ApiReturn> detail(ApiReturn apiReturn) {
		ApiReturn detail = apiReturnService.getOne(Condition.getQueryWrapper(apiReturn));
		return R.data(detail);
	}

	/**
	 * @Description 分页 接口返回码关联表
	 * @Author sjm
	 * @Date 21:18 2020/2/21 0021
	 * @param apiReturn
	 * @param query
	 * @return org.springblade.core.tool.api.R<com.baomidou.mybatisplus.core.metadata.IPage<com.dhcc.bpm.modules.sandbox.entity.ApiReturn>>
	 **/
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入apiReturn")
	@ApiLog("查询返回码关联")
	@Log(title = "查询返回码关联", businessType = BusinessType.SELECT)
	public R<IPage<ApiReturn>> list(@ApiIgnore @RequestParam Map<String, Object> apiReturn, Query query) {
		IPage<ApiReturn> pages = apiReturnService.page(Condition.getPage(query), Condition.getQueryWrapper(apiReturn,ApiReturn.class));
		return R.data(pages);
	}

	/**
	 * @Description 自定义分页 接口返回码关联表
	 * @Author sjm
	 * @Date 21:19 2020/2/21 0021
	 * @param apiReturn
	 * @param query
	 * @return org.springblade.core.tool.api.R<com.baomidou.mybatisplus.core.metadata.IPage<com.dhcc.bpm.modules.sandbox.vo.ApiReturnVO>>
	 **/
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入apiReturn")
	@ApiLog("分页查询返回码关联")
	@Log(title = "查询返回码关联", businessType = BusinessType.SELECT)
	public R<IPage<ApiReturnVO>> page(ApiReturnVO apiReturn, Query query) {
		IPage<ApiReturnVO> pages = apiReturnService.selectApiReturnPage(Condition.getPage(query), apiReturn);
		return R.data(pages);
	}

	/**
	 * @Description 自定义分页 返回码表
	 * @Author sjm
	 * @Date 21:23 2020/2/21 0021
	 * @param apiReturnCodeVO
	 * @param query
	 * @return org.springblade.core.tool.api.R<com.baomidou.mybatisplus.core.metadata.IPage<com.dhcc.bpm.modules.sandbox.vo.ApiReturnCodeVO>>
	 **/
	@GetMapping("/getPageByProductId")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "分页", notes = "传入productId")
	@ApiLog("查询返回码表")
	@Log(title = "查询返回码表", businessType = BusinessType.SELECT)
	public R<IPage<ApiReturnCodeVO>> getPageByProductId(ApiReturnCodeVO apiReturnCodeVO, Query query) {
		IPage<ApiReturnCodeVO> pages = apiReturnService.selectApiReturnCodePage(Condition.getPage(query), apiReturnCodeVO);
		return R.data(pages);
	}

	/**
	 * @Description 自定义分页 返回码表
	 * @Author sjm
	 * @Date 21:24 2020/2/21 0021
	 * @param apiReturnCodeVO
	 * @param query
	 * @return org.springblade.core.tool.api.R<com.baomidou.mybatisplus.core.metadata.IPage<com.dhcc.bpm.modules.sandbox.vo.ApiReturnCodeVO>>
	 **/
	@GetMapping("/getPageByCaseId")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "分页", notes = "传入caseId")
	@ApiLog("分页查询返回码表")
	@Log(title = "查询报文列表", businessType = BusinessType.SELECT)
	public R<IPage<ApiReturnCodeVO>> getPageByCaseId(ApiReturnCodeVO apiReturnCodeVO, Query query) {
		IPage<ApiReturnCodeVO> pages = apiReturnService.selectApiReturnCodeByCaseId(Condition.getPage(query), apiReturnCodeVO);
		return R.data(pages);
	}

	/**
	 * @Description 新增 接口返回码关联表
	 * @Author sjm
	 * @Date 21:24 2020/2/21 0021
	 * @param apiReturn
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/save")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增", notes = "传入apiReturn")
	@ApiLog("新增返回码")
	@Log(title = "接口返回码新增", businessType = BusinessType.INSERT)
	public R save(@Valid @RequestBody ApiReturn apiReturn) {
		return R.status(apiReturnService.save(apiReturn));
	}

	/**
	 * @Description 修改 接口返回码关联表
	 * @Author sjm
	 * @Date 21:24 2020/2/21 0021
	 * @param apiReturn
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/update")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "修改", notes = "传入apiReturn")
	@ApiLog("接口返回码修改")
	@Log(title = "接口返回码修改", businessType = BusinessType.UPDATE)
	public R update(@Valid @RequestBody ApiReturn apiReturn) {
		return R.status(apiReturnService.updateById(apiReturn));
	}

	/**
	 * @Description 新增或修改 接口返回码关联表
	 * @Author sjm
	 * @Date 21:25 2020/2/21 0021
	 * @param apiReturn
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/submit")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "新增或修改", notes = "传入apiReturn")
	@ApiLog("接口返回码新增或修改")
	@Log(title = "接口返回码修改", businessType = BusinessType.UPDATE)
	public R submit(@Valid @RequestBody ApiReturn apiReturn) {
		return R.status(apiReturnService.saveOrUpdate(apiReturn));
	}

	/**
	 * @Description  新增或修改 接口返回码关联表
	 * @Author sjm
	 * @Date 21:25 2020/2/21 0021
	 * @param id
	 * @param apiId
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/submitApiCode")
	@ApiOperationSupport(order = 9)
	@ApiOperation(value = "新增或修改", notes = "传入apiId,id")
	@ApiLog("接口返回码修改")
	@Log(title = "接口返回码修改", businessType = BusinessType.INSERT)
	public R submitApiCode(@ApiParam(value = "主键集合", required = true) @RequestParam Long id,
						  @ApiParam(value = "主键集合", required = true) @RequestParam Long apiId) {
		ApiReturn apiReturn = apiReturnService.selectApiReturn(apiId);
		if(apiReturn == null){
			apiReturn = new ApiReturn();
			apiReturn.setApiId(apiId);
		}
		apiReturn.setReturnCodeId(id);
		return R.status(apiReturnService.saveOrUpdate(apiReturn));
	}

}
